<?php

/*

*/

include '../include/init.php';

$uid=$_POST['u'];
$type=$_POST['type'];

if($type=="diy")
{
	include '../tools/img.php';

	$src=$_POST['src'];

	$scale=$_POST['scale']+0;
	//$imgw=$_POST['imgw']+0;
	//$imgh=$_POST['imgh']+0;
	$frameh=$_POST['frameh']+0-2;
	$framet=$_POST['framet']+0-1;
	$framel=$_POST['framel']+0-1;

	//$framet==0?$framet+=3:$framet-=3;
	//$framel==0?$framel+=3:$framel-=4;

	$sx=($framel)/$scale;
	$sy=($framet)/$scale;

	$width=($frameh)/$scale;
	
	$rotate=$_POST['rotate']+0;

	//将图片转存到上载文件夹
	if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $src, $result))
	{
		$type = $result[2];
		$new_file =ROOT."upload/temp/user".$uid."_imgheadset_".time().".".$type;
	
		//将base64编码转成图片文件
		if (file_put_contents($new_file, base64_decode(str_replace($result[1],'', $src))))
		{
			$usrdir=ROOT.'img/UserHead/user'.$uid.'/';
			if(!is_dir($usrdir))
			{
				mkdir($usrdir,777,true);
			}

			$imgsize=getimagesize($new_file);
			$sx<0?$sx=0:'';
			$sy<0?$sy=0:'';

			$sx+$width>$imgsize[0]?$sx=$imgsize[0]-$width:'';
			$sy+$width>$imgsize[1]?$sy=$imgsize[1]-$width:'';
			
			//将转存的图片切割
			$newimg_normal=image::cut_img($new_file,$sx,$sy,$width,$width,$usrdir.'user'.$uid.'_imghead_'.time().'.'.$type);
			
			//旋转图片
			if($rotate!=0)
			{
				$deg=0;
				if($rotate==1||$rotate==-3)
				{
					$deg=270;
				}
				else if($rotate==-1||$rotate==3)
				{
					$deg=90;
				}
				else if($rotate==2||$rotate==-2)
				{
					$deg=180;
				}
				$newimg_normal=image::rotate($newimg_normal,$deg,true);
			}

			

			//如果有使用滤镜，则合并滤镜
			//$newimg_normal=image::filtermix($newimg_normal,$filtertype,$filterimg,$filteropacity);

			//生成小尺寸图片
			$newimg_100=image::make_thumb($newimg_normal,100,100);
			$newimg_60=image::make_thumb($newimg_normal,60,60);
			$newimg_30=image::make_thumb($newimg_normal,30,30);

			//生成小尺寸黑白图片
			$newimg_gray=image::setgray($newimg_60);

			//删除转存图片
			unlink($new_file);

			//删除用户之前使用过的头像大图,100图,黑白图，只保留100*100规格缩略头像
			$user=new userModel();
			$imgs=$user->getsome($uid,array('imghead_normal','imghead_30','imghead_60','imghead_100','imghead_gray'));
		
			if($imgs)
			{
				foreach($imgs as $k=>$v)
				{
					if(strpos($v,'UserHead/user'.$uid)!==false)//先判断此路径是自定义路径还是公共路径，是自定义才编辑
					{
						if($k=='imghead_100')
						{
							$imghis=$user->getone($uid,'imghead_history','ownid','usr_detail');
							$user->setone($uid,'imghead_history',$imghis.$v.';','ownid','usr_detail');
							continue;
						}
						if(file_exists(ROOT.$v))
						{
							unlink(ROOT.$v);
						}
					}
				}
			}
			
			
			$newdata=array('imghead_normal'=>str_replace(ROOT,'',$newimg_normal),'imghead_30'=>str_replace(ROOT,'',$newimg_30),'imghead_60'=>str_replace(ROOT,'',$newimg_60),'imghead_100'=>str_replace(ROOT,'',$newimg_100),'imghead_gray'=>str_replace(ROOT,'',$newimg_gray));

			//将新图片链接修改到数据库，返回结果
			if($user->setsome($uid,$newdata))
			{
				die(json_encode($newdata));
			}
			else
			{
				die(json_encode('no'));
			}

		}
		else
		{
			die(json_encode('no'));
		}
	 
	}

}
else if($type=="his")
{
	include '../tools/img.php';

	$src=trim($_POST['src']);

	$scale=$_POST['scale']+0;

	$frameh=$_POST['frameh']+0>0?$_POST['frameh']+0-2:$_POST['frameh']+0;
	$framet=$_POST['framet']+0>0?$_POST['framet']+0-1:$_POST['framet']+0;
	$framel=$_POST['framel']+0>0?$_POST['framel']+0-1:$_POST['framel']+0;

	$sx=($framel)/$scale;
	$sy=($framet)/$scale;

	$width=($frameh)/$scale;

	//这个历史头像是否已经被编辑过
	$hisdiy=$_POST['hisdiy']+0;
	
	//if($width==0&&strpos($src,"img/head/")!==false)
	if($hisdiy==0)
	{
		if(file_exists(ROOT.$src))
		{
			//生成小尺寸图片
			$newimg_100=$newimg_normal=ROOT.$src;
			$newimg_60=image::make_thumb($newimg_normal,60,60);
			$newimg_30=image::make_thumb($newimg_normal,30,30);

			//生成小尺寸黑白图片
			$newimg_gray=image::setgray($newimg_60);
		}
		else
		{
			die(json_encode('no'));
		}
	
	}
	else
	{
		$rotate=$_POST['rotate']+0;
		$srcs=explode('.',$src);
		$type =$srcs[count($srcs)-1];
		$new_file =ROOT.$src;
		
		if(file_exists($new_file))
		{
			$imgsize=getimagesize($new_file);

			$width==0?$width=$imgsize[0]:'';

			$sx<0?$sx=0:'';
			$sx+$width>$imgsize[0]?$sx=$imgsize[0]-$width:'';
			$sy<0?$sy=0:'';
			$sy+$width>$imgsize[1]?$sy=$imgsize[1]-$width:'';
			
			$usrdir=ROOT.'img/UserHead/user'.$uid.'/';
			if(!is_dir($usrdir))
			{
				mkdir($usrdir,777,true);
			}

			//将转存的图片切割
			$newimg_normal=image::cut_img($new_file,$sx,$sy,$width,$width,$usrdir.'user'.$uid.'_imghead_'.time().'.'.$type);

			if($rotate!=0)
			{
				$deg=0;
				if($rotate==1||$rotate==-3)
				{
					$deg=270;
				}
				else if($rotate==-1||$rotate==3)
				{
					$deg=90;
				}
				else if($rotate==2||$rotate==-2)
				{
					$deg=180;
				}
				$newimg_normal=image::rotate($newimg_normal,$deg,false);

			}

			//如果有使用滤镜，则合并滤镜
			//$newimg_normal=image::filtermix($newimg_normal,$filtertype,$filterimg,$filteropacity);

			//生成小尺寸图片
			$newimg_100=image::make_thumb($newimg_normal,100,100);
			$newimg_60=image::make_thumb($newimg_normal,60,60);
			$newimg_30=image::make_thumb($newimg_normal,30,30);

			//生成小尺寸黑白图片
			$newimg_gray=image::setgray($newimg_60);
		}
		else
		{
			die(json_encode('no'));
		}
		

	}

	//删除用户之前使用过的头像大图,100图,黑白图，只保留100*100规格缩略头像
	$user=new userModel();
	$imgs=$user->getsome($uid,array('imghead_normal','imghead_30','imghead_60','imghead_100','imghead_gray'));

	if($imgs)
	{
		foreach($imgs as $k=>$v)
		{
			if(strpos($v,'UserHead/user'.$uid)!==false)//先判断此路径是自定义路径还是公共路径，是自定义才编辑
			{
				if($k=='imghead_100')
				{
					$imghis=$user->getone($uid,'imghead_history','ownid','usr_detail');
					$user->setone($uid,'imghead_history',$imghis.$v.';','ownid','usr_detail');
					continue;
				}
				if(file_exists(ROOT.$v))
				{
					unlink(ROOT.$v);
				}
			}
		}
	}
	
	
	$newdata=array('imghead_normal'=>str_replace(ROOT,'',$newimg_normal),'imghead_30'=>str_replace(ROOT,'',$newimg_30),'imghead_60'=>str_replace(ROOT,'',$newimg_60),'imghead_100'=>str_replace(ROOT,'',$newimg_100),'imghead_gray'=>str_replace(ROOT,'',$newimg_gray));

	//将新图片链接修改到数据库，返回结果
	if($user->setsome($uid,$newdata))
	{
		die(json_encode($newdata));
	}
	else
	{
		die(json_encode('no'));
	}

}
else if($type=="sysdiy")
{
	include '../tools/img.php';

	$src=trim($_POST['src']);
	$hid=$_POST['h']+0;

	$user=new userModel();
	$uh=new userheadModel();

	$viper=$user->getone($uid,'isvip');
	$imghead=$uh->getrow($hid);

	if($imghead['url']!=$src)
	{
		die(json_encode('no'));
	}

	//if($imghead&&$viper!=false)
	//{
		if($imghead['isvip']==$viper||$imghead['isfree']==1)
		{

			$scale=$_POST['scale']+0;

			$frameh=$_POST['frameh']+0>0?$_POST['frameh']+0-2:$_POST['frameh']+0;
			$framet=$_POST['framet']+0>0?$_POST['framet']+0-1:$_POST['framet']+0;
			$framel=$_POST['framel']+0>0?$_POST['framel']+0-1:$_POST['framel']+0;

			$sx=($framel)/$scale;
			$sy=($framet)/$scale;

			$width=($frameh)/$scale;

			
			$rotate=$_POST['rotate']+0;
			$srcs=explode('.',$src);
			$type =$srcs[count($srcs)-1];
			$new_file =ROOT.$src;
			
			if(file_exists($new_file))
			{
				$imgsize=getimagesize($new_file);

				$width==0?$width=$imgsize[0]:'';

				$sx<0?$sx=0:'';
				$sx+$width>$imgsize[0]?$sx=$imgsize[0]-$width:'';
				$sy<0?$sy=0:'';
				$sy+$width>$imgsize[1]?$sy=$imgsize[1]-$width:'';
				
				$usrdir=ROOT.'img/UserHead/user'.$uid.'/';
				if(!is_dir($usrdir))
				{
					mkdir($usrdir,777,true);
				}

				//将转存的图片切割
				$newimg_normal=image::cut_img($new_file,$sx,$sy,$width,$width,$usrdir.'user'.$uid.'_imghead_'.time().'.'.$type);

				if($rotate!=0)
				{
					$deg=0;
					if($rotate==1||$rotate==-3)
					{
						$deg=270;
					}
					else if($rotate==-1||$rotate==3)
					{
						$deg=90;
					}
					else if($rotate==2||$rotate==-2)
					{
						$deg=180;
					}
					$newimg_normal=image::rotate($newimg_normal,$deg,false);

				}

				//如果有使用滤镜，则合并滤镜
				//$newimg_normal=image::filtermix($newimg_normal,$filtertype,$filterimg,$filteropacity);

				//生成小尺寸图片
				$newimg_100=image::make_thumb($newimg_normal,100,100);
				$newimg_60=image::make_thumb($newimg_normal,60,60);
				$newimg_30=image::make_thumb($newimg_normal,30,30);

				//生成小尺寸黑白图片
				$newimg_gray=image::setgray($newimg_60);
			}
			else
			{
				die(json_encode('no'));
			}
				

			//删除用户之前使用过的头像大图,100图,黑白图，只保留100*100规格缩略头像
			$user=new userModel();
			$imgs=$user->getsome($uid,array('imghead_normal','imghead_30','imghead_60','imghead_100','imghead_gray'));

			if($imgs)
			{
				foreach($imgs as $k=>$v)
				{
					if(strpos($v,'UserHead/user'.$uid)!==false)//先判断此路径是自定义路径还是公共路径，是自定义才编辑
					{
						if($k=='imghead_100')
						{
							$imghis=$user->getone($uid,'imghead_history','ownid','usr_detail');
							$user->setone($uid,'imghead_history',$imghis.$v.';','ownid','usr_detail');
							continue;
						}
						if(file_exists(ROOT.$v))
						{
							unlink(ROOT.$v);
						}
					}
				}
			}
			
			
			$newdata=array('imghead_normal'=>str_replace(ROOT,'',$newimg_normal),'imghead_30'=>str_replace(ROOT,'',$newimg_30),'imghead_60'=>str_replace(ROOT,'',$newimg_60),'imghead_100'=>str_replace(ROOT,'',$newimg_100),'imghead_gray'=>str_replace(ROOT,'',$newimg_gray));

			//将新图片链接修改到数据库，返回结果
			if($user->setsome($uid,$newdata))
			{
				die(json_encode($newdata));
			}
			else
			{
				die(json_encode('no'));
			}
		}
	//}
	//die(json_encode('no'));

}
else if($type=="sys")
{
	include '../tools/img.php';

	$src=trim($_POST['src']);
	$hid=$_POST['h']+0;

	$user=new userModel();
	$uh=new userheadModel();

	$viper=$user->getone($uid,'isvip');
	$imghead=$uh->getrow($hid);

	if($imghead['url']!=$src)
	{
		die(json_encode('no'));
	}

	if($imghead&&$viper!=false)
	{
		if($imghead['isvip']==$viper||$imghead['isfree']==1)
		{

			$usrdir=ROOT.'img/UserHead/user'.$uid.'/';
			if(!is_dir($usrdir))
			{
				mkdir($usrdir,777,true);
			}

			$newimg_gray=image::setgray(ROOT.$src);
			
			$gray=explode('/',$newimg_gray);
			$gray=$usrdir.$gray[count($gray)-1];
			rename($newimg_gray,$gray);

			//删除用户之前使用过的头像大图,100图,黑白图，只保留100*100规格缩略头像
			$user=new userModel();
			$imgs=$user->getsome($uid,array('imghead_normal','imghead_30','imghead_60','imghead_100','imghead_gray'));

			if($imgs)
			{
				foreach($imgs as $k=>$v)
				{
					if(strpos($v,'UserHead/user'.$uid)!==false)//先判断此路径是自定义路径还是公共路径，是自定义才编辑
					{
						if($k=='imghead_100')
						{
							$imghis=$user->getone($uid,'imghead_history','ownid','usr_detail');
							$user->setone($uid,'imghead_history',$imghis.$v.';','ownid','usr_detail');
							continue;
						}
						if(file_exists(ROOT.$v))
						{
							unlink(ROOT.$v);
						}
					}
				}
			}
			
			
			$newdata=array('imghead_normal'=>str_replace(ROOT,'',$src),'imghead_30'=>str_replace(ROOT,'',$src),'imghead_60'=>str_replace(ROOT,'',$src),'imghead_100'=>str_replace(ROOT,'',$src),'imghead_gray'=>str_replace(ROOT,'',$gray));

			//将新图片链接修改到数据库，返回结果
			if($user->setsome($uid,$newdata))
			{
				die(json_encode($newdata));
			}
			else
			{
				die(json_encode('no'));
			}

		}
	}
	die(json_encode('no'));




}